﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EditCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Label10.Visible = false;
            

            Button2.Attributes.Add("onclick","confirm('Are you sure you want to proceed?')");
            LoadData();
          
        }
      
      
    
      
    }

    private void LoadData()
    {
        int id = int.Parse(Request.QueryString["id"].ToString());
        ManagemetSubService m = new ManagemetSubService();

        ORANGE_PROJECTModel.SubCategory s = (ORANGE_PROJECTModel.SubCategory)m.GetSubService(id);
        DropDownList1.DataSource = m.GetServiceParent();
        DropDownList1.DataBind();
        DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(s.ServiceCategoryID + ""));

        txtID.Text = (id).ToString();
        txtName.Text = s.SubName;
        txtDes.Text = s.Description;
        Image2.ImageUrl = "./images/" + s.UrlImage;
    }

    
    protected void Button1_Click(object sender, EventArgs e)
    {
        string filename;
        int id = int.Parse(Request.QueryString["id"].ToString());
        ManagemetSubService m = new ManagemetSubService();
        ORANGE_PROJECTModel.SubCategory s = (ORANGE_PROJECTModel.SubCategory)m.GetSubService(id);
        filename = s.UrlImage;
        if (FileUpload.FileName.Length > 0)
        {
            filename = FileUpload.FileName;
        }
        if (m.UpdateSubService(Int32.Parse(txtID.Text), txtName.Text, txtDes.Text, Int32.Parse(DropDownList1.SelectedValue), filename))
        {
            if (FileUpload.FileName.Length > 0)
            {
                FileUpload.SaveAs(Server.MapPath("images/" + FileUpload.FileName));
            }
            LoadData();
            Label10.Visible = true;
            Label10.Text = "<span style='color:green;font-weight: bold;'>Update Succerfully :)</span>";
            
        }
        else {
            Label10.Visible = true;
            Label10.Text = "<span style='color:red;font-weight: bold;'>Update Failed (:</span>";
        }
        
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int id = int.Parse(Request.QueryString["id"].ToString());
        ManagemetSubService m = new ManagemetSubService();
        if (m.DeleteSubService(id))
        {
            Response.Redirect("ManagementSubService.aspx");
        }
    }
}